package com.sale.salesystem.controller;

import com.sale.salesystem.dto.DashboardCreateDTO;
import com.sale.salesystem.dto.DashboardChartDTO;
import com.sale.salesystem.dto.ChartOptionDTO;
import com.sale.salesystem.service.DashboardService;
import com.sale.salesystem.service.PermissionService;
import com.sale.salesystem.vo.DashboardDetailVO;
import com.sale.salesystem.vo.ResultVO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 仪表板管理控制器
 */
@RestController
@RequestMapping("/api/dashboard/manage")
@RequiredArgsConstructor
public class DashboardManageController {

    private final DashboardService dashboardService;
    private final PermissionService permissionService;

    /**
     * 创建仪表板
     *
     * @param createDTO 创建仪表板的数据传输对象
     * @return 创建的仪表板ID
     */
    @PostMapping("/create")
    public ResultVO<Long> createDashboard(@RequestBody @Valid DashboardCreateDTO createDTO) {
        // 检查权限
        if (!permissionService.isAdmin(createDTO.getOwnerId())) {
            return ResultVO.error("只有管理员可以创建仪表板");
        }
        return ResultVO.success(dashboardService.createDashboard(createDTO));
    }

    /**
     * 获取仪表板详情
     *
     * @param id 仪表板ID
     * @return 仪表板详细信息
     */
    @GetMapping("/detail/{id}")
    public ResultVO<DashboardDetailVO> getDashboardDetail(@PathVariable String id) {
        return ResultVO.success(dashboardService.getDashboardDetail(Long.valueOf(id)));
    }

    /**
     * 更新仪表板状态
     *
     * @param id     仪表板ID
     * @param status 新状态
     * @return 操作结果
     */
    @PutMapping("/{id}/status")
    public ResultVO<Void> updateStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        dashboardService.updateDashboardStatus(id, status);
        return ResultVO.success();
    }

    /**
     * 添加图表
     *
     * @param dashboardId 仪表板ID
     * @param chartDTO    图表数据
     * @return 操作结果
     */
    @PostMapping("/{dashboardId}/charts")
    public ResultVO<Void> addChart(
            @PathVariable Long dashboardId,
            @RequestBody @Valid DashboardChartDTO chartDTO) {
        dashboardService.addChart(dashboardId, chartDTO);
        return ResultVO.success();
    }

    /**
     * 删除图表
     *
     * @param dashboardId 仪表板ID
     * @param chartId     图表ID
     * @return 操作结果
     */
    @DeleteMapping("/{dashboardId}/charts/{chartId}")
    public ResultVO<Void> deleteChart(
            @PathVariable Long dashboardId,
            @PathVariable Long chartId) {
        dashboardService.deleteChart(chartId);
        return ResultVO.success();
    }

    /**
     * 更新图表顺序
     *
     * @param dashboardId 仪表板ID
     * @param chartId     图表ID
     * @param order       新的顺序
     * @return 操作结果
     */
    @PutMapping("/{dashboardId}/charts/{chartId}/order")
    public ResultVO<Void> updateChartOrder(
            @PathVariable Long dashboardId,
            @PathVariable Long chartId,
            @RequestParam Integer order) {
        dashboardService.updateChartOrder(chartId, order);
        return ResultVO.success();
    }

    /**
     * 获取用户的仪表板列表
     *
     * @param userId 用户ID
     * @return 仪表板列表
     */
    @GetMapping("/user/{userId}")
    public ResultVO<List<DashboardDetailVO>> getUserDashboards(@PathVariable Long userId) {
        return ResultVO.success(dashboardService.getUserDashboards(userId));
    }

    /**
     * 获取已发布的仪表板列表
     *
     * @return 已发布的仪表板列表
     */
    @GetMapping("/published")
    public ResultVO<List<DashboardDetailVO>> getPublishedDashboards() {
        return ResultVO.success(dashboardService.getPublishedDashboards());
    }

    /**
     * 获取图表选项
     *
     * @return 图表选项数据
     */
    @GetMapping("/chart-options")
    public ResultVO<ChartOptionDTO> getChartOptions() {
        return ResultVO.success(dashboardService.getChartOptions());
    }

}